package com.ecr.hub.database.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import com.ecr.hub.model.wagon.Wagon;
import com.ecr.hub.model.wagon.WagonStatus;
import com.ecr.hub.model.wagon.WagonType;

public class MapperHub extends MapperComposition {

	protected static ParameterizedRowMapper<Wagon> mapperWagon = new ParameterizedRowMapper<Wagon>() {
		public Wagon mapRow(ResultSet rs, int rowNum) throws SQLException {
			Wagon wagon = new Wagon();

			wagon.setId(rs.getInt("wagon_id")); // TODO ALERT
			wagon.setExtId(rs.getString("ext_id"));
			wagon.setHexColor(rs.getString("destination_hex_color"));
			// La position actuelle du wagon (complete).
			wagon.getCurrentPos().setPosRailId(rs.getInt("pos_rail_id"));
			wagon.getCurrentPos().setPosReal(rs.getBoolean("pos_real"));
			wagon.getCurrentPos().setPosNum(rs.getInt("pos_num"));
			// Pour les demandes de changement on recopie la position actuelle.
			wagon.getNextPos().setPosRailId(rs.getInt("pos_rail_id"));
			wagon.getNextPos().setPosNum(rs.getInt("pos_num"));
			wagon.getNextPos().setPosReal(false);
			wagon.setFromRail(rs.getString("pos_from_rail"));
			wagon.setLading(rs.getString("lading"));
			wagon.setDestinationId(rs.getInt("destination_id"));
			wagon.setWeightLading(rs.getFloat("weight_lading"));
			wagon.getCurrentStatus().setId(rs.getInt("status_id"));
			wagon.getCurrentStatus().setLabel(rs.getString("status_label"));
			wagon.getNextStatus().setId(rs.getInt("status_id"));
			wagon.getNextStatus().setLabel(rs.getString("status_label"));
			wagon.getType().setId(rs.getInt("type_id"));
			wagon.getType().setLabel(rs.getString("type_label"));
			wagon.getType().setTotalLength(rs.getFloat("total_length"));
			wagon.getType().setTrailer(rs.getBoolean("trailer"));
			wagon.getType().setCurbWeight(rs.getFloat("curb_weight"));
			wagon.getType().setLabel(rs.getString("type_label"));
			wagon.setTcmsTrainId(rs.getString("tcms_train_id"));
			wagon.setDestinationLabel(rs.getString("destination_label"));
			wagon.setSillonExtId(rs.getString("sillon_ext_id"));
			wagon.setSillonId(rs.getInt("sillon_id"));
			wagon.setFuelLevel(rs.getString("fuel_level"));
			wagon.setAte(rs.getString("ate"));
			wagon.setBrakeForce(rs.getFloat("brake_force"));
			wagon.setComment(rs.getString("comment"));
			wagon.setOwner(rs.getString("owner"));
			wagon.setRiv(rs.getString("riv"));
			return wagon;
		}
	};
	
	// Juste utilisé pour une recherche dans la table hub_wagon.
	protected static ParameterizedRowMapper<Wagon> mapperWagonBase = new ParameterizedRowMapper<Wagon>() {
		public Wagon mapRow(ResultSet rs, int rowNum) throws SQLException {
			Wagon wagon = new Wagon();

			wagon.setId(rs.getInt("id"));
			wagon.setExtId(rs.getString("ext_id"));
			wagon.setOwner(rs.getString("owner"));
			wagon.getType().setId(rs.getInt("type_id"));
			wagon.setRiv(rs.getString("riv"));
			return wagon;
		}
	};

	protected static ParameterizedRowMapper<WagonStatus> mapperWagonStatus = new ParameterizedRowMapper<WagonStatus>() {
		public WagonStatus mapRow(ResultSet rs, int rowNum) throws SQLException {
			WagonStatus status = new WagonStatus();
			status.setId(rs.getInt("id"));
			status.setLabel(rs.getString("label"));
			status.setTrailer(rs.getBoolean("trailer"));
			status.setComment(rs.getString("comment"));
			return status;
		}
	};

	protected static ParameterizedRowMapper<WagonType> mapperWagonType = new ParameterizedRowMapper<WagonType>() {
		public WagonType mapRow(ResultSet rs, int rowNum) throws SQLException {
			WagonType type = new WagonType();
			type.setId(rs.getInt("id"));
			type.setAt100(rs.getInt("at_100"));
			type.setBt100(rs.getInt("bt_100"));
			type.setComment(rs.getString("comment"));
			type.setCt100(rs.getInt("ct_100"));
			type.setCurbWeight(rs.getFloat("curb_weight"));
			type.setCurveradius(rs.getInt("curve_radius"));
			type.setDiamAxle(rs.getFloat("diam_axle"));
			type.setDistPivot(rs.getFloat("dist_pivot"));
			type.setLabel(rs.getString("label"));
			type.setNbAxle(rs.getInt("nb_axle"));
			type.setSpace_axle(rs.getFloat("space_axle"));
			type.setTotalLength(rs.getFloat("total_length"));
			type.setTrailer(rs.getBoolean("trailer"));
			type.setTypeAxle(rs.getString("type_axle"));
			return type;
		}
	};
}
